#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php_pushgateway.h"

#include "src/push_init.h"

ZEND_DECLARE_MODULE_GLOBALS(pushgateway)

#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE()  \
	ZEND_PARSE_PARAMETERS_START(0, 0) \
	ZEND_PARSE_PARAMETERS_END()
#endif

// 绑定配置到结构体
PHP_INI_BEGIN()
	STD_PHP_INI_BOOLEAN("pushgateway.debug", "0", PHP_INI_ALL, OnUpdateBool, debug, zend_pushgateway_globals, pushgateway_globals)
	STD_PHP_INI_BOOLEAN("pushgateway.enable", "0", PHP_INI_ALL, OnUpdateBool, enable, zend_pushgateway_globals, pushgateway_globals)
	STD_PHP_INI_ENTRY("pushgateway.interval", "15", PHP_INI_ALL, OnUpdateLong, interval, zend_pushgateway_globals, pushgateway_globals)
	STD_PHP_INI_ENTRY("pushgateway.timeout", "5", PHP_INI_ALL, OnUpdateLong, timeout, zend_pushgateway_globals, pushgateway_globals)
	STD_PHP_INI_ENTRY("pushgateway.connecttimeout", "5", PHP_INI_ALL, OnUpdateLong, connecttimeout, zend_pushgateway_globals, pushgateway_globals)
	STD_PHP_INI_ENTRY("pushgateway.basedir", "/www", PHP_INI_ALL, OnUpdateString, basedir, zend_pushgateway_globals, pushgateway_globals)
	STD_PHP_INI_ENTRY("pushgateway.host", "http://127.0.0.1:11800", PHP_INI_ALL, OnUpdateString, host, zend_pushgateway_globals, pushgateway_globals)
	STD_PHP_INI_ENTRY("pushgateway.bucket", "0.0005,0.001,0.0025,0.005,0.01,0.025,0.05,0.1,0.25,0.5,1,2.5,5,10", PHP_INI_ALL, OnUpdateString, bucket, zend_pushgateway_globals, pushgateway_globals)
	STD_PHP_INI_ENTRY("pushgateway.output", "/tmp/php_pushgateway.log", PHP_INI_ALL, OnUpdateString, output, zend_pushgateway_globals, pushgateway_globals)
PHP_INI_END()

// 设置配置默认值
static void php_pushgateway_init_globals(zend_pushgateway_globals *pushgateway_globals)
{
	pushgateway_globals->debug = 0;
	pushgateway_globals->enable = 0;
	pushgateway_globals->interval = 0;
	pushgateway_globals->host = nullptr;
	pushgateway_globals->timeout = 0;
	pushgateway_globals->connecttimeout = 0;
	pushgateway_globals->basedir = nullptr;
	pushgateway_globals->bucket = nullptr;
	pushgateway_globals->output = nullptr;
}

PHP_FUNCTION(helloworld)
{
	php_printf("hello world!\n");
	RETURN_TRUE;
}

PHP_MINIT_FUNCTION(pushgateway)
{
	// 初始化模块配置
	ZEND_INIT_MODULE_GLOBALS(pushgateway, php_pushgateway_init_globals, NULL);
	// 读取 php.ini 配置文件
	REGISTER_INI_ENTRIES();

	// 模块加载初始化
	if (PUSHGATEWAY_G(enable))
	{
		init_minit();
	}

	return SUCCESS;
}

PHP_MSHUTDOWN_FUNCTION(pushgateway)
{
	// 移除配置
	UNREGISTER_INI_ENTRIES();

	// 模块结束
	if (PUSHGATEWAY_G(enable))
	{
		init_mshutdown();
	}

	return SUCCESS;
}

PHP_RINIT_FUNCTION(pushgateway)
{
#if defined(ZTS) && defined(COMPILE_DL_PUSHGATEWAY)
	ZEND_TSRMLS_CACHE_UPDATE();
#endif

	// 请求初始化
	if (PUSHGATEWAY_G(enable))
	{
		init_rinit();
	}

	return SUCCESS;
}

PHP_RSHUTDOWN_FUNCTION(pushgateway)
{
	// 请求结束
	if (PUSHGATEWAY_G(enable))
	{
		init_rshutdown();
	}

	return SUCCESS;
}

PHP_MINFO_FUNCTION(pushgateway)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "pushgateway support", "enabled");
	php_info_print_table_end();
}

static const zend_module_dep pushgateway_deps[] = {
	ZEND_MOD_REQUIRED("mysqlnd")
	ZEND_MOD_REQUIRED("mysqli")
	ZEND_MOD_REQUIRED("pdo")
	ZEND_MOD_REQUIRED("curl")
	ZEND_MOD_END
};

static const zend_function_entry pushgateway_functions[] = {
	PHP_FE(helloworld, NULL)
	PHP_FE_END
};

zend_module_entry pushgateway_module_entry = {
	STANDARD_MODULE_HEADER_EX,
	NULL,
    pushgateway_deps,
	"pushgateway",				/* Extension name */
	pushgateway_functions,		/* zend_function_entry */
	PHP_MINIT(pushgateway),		/* PHP_MINIT - Module initialization */
	PHP_MSHUTDOWN(pushgateway), /* PHP_MSHUTDOWN - Module shutdown */
	PHP_RINIT(pushgateway),		/* PHP_RINIT - Request initialization */
	PHP_RSHUTDOWN(pushgateway), /* PHP_RSHUTDOWN - Request shutdown */
	PHP_MINFO(pushgateway),		/* PHP_MINFO - Module info */
	PHP_PUSHGATEWAY_VERSION,	/* Version */
	STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_PUSHGATEWAY
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(pushgateway)
#endif
